<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8">
		<title>添加权限</title>
		<link rel="stylesheet" type="text/css" href="/webapp/css/easyui.css">
	    <link rel="stylesheet" type="text/css" href="/webapp/css/icon.css">
        <link rel="stylesheet" type="text/css" href="/webapp/css/color.css">
        <link rel="stylesheet" type="text/css" href="/webapp/css/public.css">
	    <script type="text/javascript" src="/webapp/js/lib/jquery.min.js"></script>
	    <script type="text/javascript" src="/webapp/js/lib/jquery.easyui.min.js"></script>
        <script type="text/javascript" src="/webapp/js/lib/jquery.global.settings.js"></script>
	    <script type="text/javascript" src="/webapp/js/lib/ajaxfileupload.js"></script>
	    <script type="text/javascript" src="/webapp/js/common/config.js"></script>
	</head>
	<body>
		<div class="easyui-container-fluid">
			<div class="easyui-panel" style="border: none;">
                <div id="nav" class="nav">

                </div>
                <div class="easyui-panel" style="border:none;">
                       <form id="ff" class="easyui-form" method="post">
                              <div style="margin-bottom:20px">
                                  <label class="item-label"><span class="item-required">*</span>名称：</label>
                                  <input class="easyui-textbox" name="name" style="" data-options="required:true">
                              </div>
                              <div style="margin-bottom:20px">
                                  <label class="item-label">URL：</label>
                                  <input class="easyui-textbox" name="url" style="width:274px;" data-options="">
                              </div>
                              <div style="margin-bottom:20px">
                                  <label class="item-label">父级：</label>
                                  <select id="parentId" class="easyui-combobox" name="parentId" style="width:174px" data-options="panelHeight:150">
                                  </select>
                              </div>
                               <div style="margin-bottom:20px">
                                   <label class="item-label"><span class="item-required">*</span>菜单等级：</label>
                                   <select id="level" class="easyui-combobox" name="level" style="width:174px" data-options="panelHeight:'auto',required:true">
                                       <option value="1" selected>一级菜单</option>
                                       <option value="2">二级菜单</option>
                                       <option value="3">三级菜单</option>
                                   </select>
                               </div>
                              <div style="margin-bottom:20px">
                               <label class="item-label"><span class="item-required">*</span>是否显示：</label>
                               <select id="isMenu" class="easyui-combobox" name="isMenu" style="width:174px" data-options="panelHeight:'auto',required:true">
                                   <option value="true" selected>是</option>
                                   <option value="false">否</option>
                               </select>
                           </div>
                           <div style="margin-left: 100px;margin-bottom:20px;">
                               <a href="javascript:void(0)" class="easyui-linkbutton c8" onclick="submitForm()" style="width:80px">提交</a>
                               <a href="javascript:void(0)" class="easyui-linkbutton" onclick="clearForm()" style="width:80px">取消</a>
                           </div>
                       </form>
                </div>
              </div>
        </div>
	</body>
       <script type="text/javascript">
          var id = getQueryParameter("id");
          function submitForm(){
             $('#ff').form('submit',{
                onSubmit:function(){
                   var valid = $(this).form('enableValidation').form('validate');
                   if(valid){
                        var data = getFormData(document.getElementById("ff"));
                        var method = "post";
                        if(id){
                           method = "patch";
                           data.id = id;
                        }
                        $.ajax({
                            url:config.url+"/permission",
                            type:method,
                            data:JSON.stringify(data),
                            dataType:'json',
                            contentType:'application/json;charset=UTF-8',
                            xhrFields:{withCredentials:true},
                            beforeSend:function(){
                                $.messager.progress({
                                    title:"请稍等",
                                    interval:50,
                                    text:"操作进行中..."
                                });
                            },
                            complete:function(){
                                $.messager.progress("close");
                            },
                            success:function(data){
                                 if(data.code==200){
                                        $.messager.alert("提示","操作成功","info",function () {
                                            window.location.href = "/webapp/pages/system/permission/list.html";
                                        });
                                 }
                            },
                            error:function(XHRHttpRequest,textStatus,errorThrown){
                                $.messager.alert("提示","操作失败","error");
                            }
                        });
                   }
                   return false;
                }
             });

          }
          function clearForm(){
              window.location.href="/webapp/pages/system/permission/list.html";
          }
          $(function () {
             setNav();
             if(id){
                 $.when(condition()).done(function () {
                     $.ajax({
                        url:config.url+"/permission",
                        type:"get",
                        data:{id:id},
                        xhrFields:{withCredentials:true},
                        success:function(data){
                            if(data){
                                $("#ff").form("load",changeProperty2String(data.content[0]));
                            }
                        },
                        error:function(XMLHttpRequest,textStatus,errorThrown){
                            //
                        }
                     });
                 });
             }else{
                 condition();
             }
             function condition(){
                 var dtd = $.Deferred();
                 $.ajax({
                     url:config.url+"/permission/getAll",
                     type:"get",
                     xhrFields:{withCredentials:true},
                     success:function(data){
                         if(data){
                             $(data).map(function(index,item){
                                 item['text'] = item.name;
                                 item['code'] = item.id;
                             });
                             data.unshift({"text":"","code":""});
                             $("#parentId").combobox({
                                 textFiled:"text",
                                 valueField:"code",
                                 panelHeight:300,
                                 prompt:"--请选择--",
                                 data:data
                             });
                             dtd.resolve();
                         }
                     },
                     error:function(XMLHttpRequest,textStatus,errorThrown){
                         dtd.reject();
                     }
                 });
                 return dtd.promise();
             }
          });
       </script>
</html>